﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellActionDialect.h"
#include "CellProperties.h"
#include "CellActionDelegate.h"
#include "CellTalkService.h"
#include "CellStringConverter.h"

namespace cell
{
//---------------------------------------------------------------------------
ActionDialect::ActionDialect(const String& tracker)
	: Dialect(ActionDialectFactory::FACTORY_NAME, tracker)
{
}
//---------------------------------------------------------------------------
ActionDialect::~ActionDialect(void)
{
}
//---------------------------------------------------------------------------
bool ActionDialect::translate(Primitive& primitive)
{
	if (mAction.empty())
		return false;

	Params::iterator iter = mParams.begin(), end = mParams.end();
	while (iter != end)
	{
		SubjectStuff name(iter->first);
		ObjectiveStuff value(iter->second);
		primitive.commit(name);
		primitive.commit(value);

		++iter;
	}

	PredicateStuff action(mAction);
	primitive.commit(action);

	primitive.capture(this);

	return true;
}
//---------------------------------------------------------------------------
void ActionDialect::build(Primitive* primitive)
{
	mAction = (*(primitive->predicates()->begin())).getValueAsString();

	const Primitive::SubjectList* subjects = primitive->subjects();
	const Primitive::ObjectiveList* objectives = primitive->objectives();

	Primitive::SubjectList::const_iterator siter = subjects->begin(), send = subjects->end();
	Primitive::ObjectiveList::const_iterator oiter = objectives->begin();
	while (siter != send)
	{
		mParams.insert(Params::value_type((*siter).getValueAsString(), (*oiter).getValueAsString()));

		++siter;
		++oiter;
	}
}
//---------------------------------------------------------------------------
void ActionDialect::setAction(const String& action)
{
	mAction = action;
}
//---------------------------------------------------------------------------
const String& ActionDialect::getAction(void) const
{
	return mAction;
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, const char* value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, value));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, const String& value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, value));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, int value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, StringConverter::toString(value)));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, uint value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, StringConverter::toString(value)));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, long value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, StringConverter::toString(value)));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, ulong value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, StringConverter::toString(value)));
}
//---------------------------------------------------------------------------
void ActionDialect::appendParam(const String& name, bool value)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		mParams.erase(iter);
	}

	mParams.insert(Params::value_type(name, StringConverter::toString(value)));
}
//---------------------------------------------------------------------------
String ActionDialect::getParamAsString(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return iter->second;
	}
	else
	{
		return StringUtil::BLANK;
	}
}
//---------------------------------------------------------------------------
int ActionDialect::getParamAsInt(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return StringConverter::parseInt(iter->second);
	}
	else
	{
		return 0;
	}
}
//---------------------------------------------------------------------------
uint ActionDialect::getParamAsUInt(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return StringConverter::parseUnsignedInt(iter->second);
	}
	else
	{
		return 0;
	}
}
//---------------------------------------------------------------------------
long ActionDialect::getParamAsLong(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return StringConverter::parseLong(iter->second);
	}
	else
	{
		return 0;
	}
}
//---------------------------------------------------------------------------
ulong ActionDialect::getParamAsULong(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return StringConverter::parseUnsignedLong(iter->second);
	}
	else
	{
		return 0;
	}
}
//---------------------------------------------------------------------------
bool ActionDialect::getParamAsBool(const String& name)
{
	Params::iterator iter = mParams.find(name);
	if (iter != mParams.end())
	{
		return StringConverter::parseBool(iter->second);
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------
bool ActionDialect::existParam(const String& name)
{
	return (mParams.find(name) != mParams.end());
}
//---------------------------------------------------------------------------
void ActionDialect::copyParams(ActionDialect& target)
{
	Params::iterator iter = mParams.begin(), end = mParams.end();
	while (iter != end)
	{
		target.appendParam(iter->first, iter->second);
		++iter;
	}
}
//---------------------------------------------------------------------------
void ActionDialect::act(ActionDelegate* delegate)
{
	TalkService::getSingleton().doAction(delegate, this);
}



//---------------------------------------------------------------------------
const String ActionDialectFactory::FACTORY_NAME = "ActionDialect";

//---------------------------------------------------------------------------
ActionDialectFactory::ActionDialectFactory(void)
	: DialectFactory()
{
}
//---------------------------------------------------------------------------
ActionDialectFactory::~ActionDialectFactory(void)
{
}
//---------------------------------------------------------------------------
Dialect* ActionDialectFactory::createInstance(const String& tracker)
{
	return CELL_NEW ActionDialect(tracker);
}
//---------------------------------------------------------------------------
void ActionDialectFactory::destroyInstance(Dialect* instance)
{
	CELL_DELETE static_cast<ActionDialect*>(instance);
}
//---------------------------------------------------------------------------
void ActionDialectFactory::initMetaData(void) const
{
	mMetaData.name = FACTORY_NAME;
	mMetaData.description = "Action Dialect";
}
} // end namespace cell
